package com.cg.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @Author: cg
 * @Date: Create IN 2017/6/23 14:53
 * @Email: cg_666666@163.com
 */
public class JSSDKUtil {
    /**
     * jsapi
     *
     * @param jsapi_ticket
     * @param url
     * @return
     */
    public static Map<String, String> sign(String jsapi_ticket, String url) {
        Map<String, String> ret = new HashMap<String, String>();
        String nonce_str = create_nonce_str();
        String timestamp = create_timestamp();
        String string1;
        String signature = "";

        //注意这里参数名必须全部小写，且必须有序
        string1 = "jsapi_ticket=" + jsapi_ticket +
                "&noncestr=" + nonce_str +
                "&timestamp=" + timestamp +
                "&url=" + url;
        System.out.println(string1);

        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        ret.put("url", url);
        ret.put("jsapi_ticket", jsapi_ticket);
        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);

        return ret;
    }

    /**
     * 卡券签名
     * @param nonce_str
     * @param times_tamp
     * @param api_ticket
     * @param card_type
     * @param card_id
     * @param app_id
     * @return
     */
    public static Map<String, String> cardsign(String nonce_str, String times_tamp,String api_ticket, String card_type, String card_id, String app_id) {
        List<String> list = new ArrayList<String>();
        list.add(times_tamp);
        list.add(nonce_str);
        list.add(card_type);
        list.add(api_ticket);
        list.add(card_id);
        list.add(app_id);
        Collections.sort(list);
        String sourcestr = "";
        for (String o : list) {
            sourcestr += o;
        }
        System.out.println(sourcestr);
        String cardSign = "";
        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(sourcestr.getBytes("UTF-8"));
            cardSign = byteToHex(crypt.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Map<String,String> map=new HashMap<>();
        map.put("cardType",card_type);
        map.put("cardId",card_id);
        map.put("timestamp",times_tamp);
        map.put("nonceStr",nonce_str);
        map.put("signType","SHA1");
        map.put("cardSign",cardSign);
        return map;
    }

    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    private static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }

    private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }
}
